package site.lyweb.controller;

import site.lyweb.domain.dto.PageDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import site.lyweb.domain.dto.Teacher;
import site.lyweb.domain.dto.TeacherDTO;
import site.lyweb.domain.query.TeacherQuery;
import site.lyweb.result.RestResult;
import site.lyweb.service.TeacherService;
import site.lyweb.utils.UUIDUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @Author 林瑶
 * @Description TODO
 * @Date 2024/6/20 0:32
 */
@CrossOrigin(origins = "*")
@RestController
@Tag(name="教师管理模块")
@Slf4j
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;
    /**
     * 分页查询老师列表数据
     *
     * @return
     */

    @Operation(summary = "分页查询老师列表数据")
    @GetMapping(value = "/tea_info/getTeacherByPage")
    public RestResult getTeacherByPage( TeacherQuery teacherQuery) {


        //分页查询的结果用PageDTO进行封装
        PageDTO<TeacherDTO> pageDTO = teacherService.getTeacherByPage(0, teacherQuery);

        //有的时候，查询可以不判断成功与失败，不管查没查到数据，都返回200，页面上去控制显示
        return RestResult.SUCCESS(pageDTO);
    }
    /**
     * 根据id查找老师信息表中信息
     */
    @Operation(summary = "根据id查找老师信息表中信息")
    @Parameters({
            @Parameter(name = "id",description = "id(主键)",in = ParameterIn.PATH)
    })
    @GetMapping("/tea_info/getById/{id}")
    public RestResult queryStudentById(@PathVariable("id") String id){
        Teacher byId = teacherService.getById(id);

        return RestResult.SUCCESS(byId);
    }
    /**
     * 查找全部老师信息表中信息
     */
    @Operation(summary = "查找全部老师信息表中信息")
    @GetMapping("/tea_info/getAll")
    public RestResult queryStudentAll(){
        return RestResult.SUCCESS(teacherService.list());
    }
    /**
     * 根据id删除老师信息表中信息
     */
    @Operation(summary = "根据id删除老师信息表中信息")
    @Parameters({
            @Parameter(name = "id",description = "id(主键)",in = ParameterIn.PATH)
    })
    @CrossOrigin
    @PostMapping("/tea_info/del/{id}")
    public RestResult delStudentById(@PathVariable("id") String id){
        return teacherService.removeById(id) ?RestResult.SUCCESS():RestResult.FAIL();
    }
    /**
     * 根据id批量老师信息表中信息
     */
    @Operation(summary = "根据ids删除老师信息表中信息")
    @Parameters({
            @Parameter(name = "ids",description = "id(主键)",in = ParameterIn.PATH)
    })
    @CrossOrigin
    @PostMapping("/tea_info/batchDel/{ids}")
    public RestResult batchDelStudentById(@PathVariable("ids") String ids){
        //ids = "1,2,5,7,10,12" , ids.split(",")是把ids转成数组, Arrays.asList()是把数组转成List
        List<String> idList = Arrays.asList(ids.split(","));

        return  teacherService.removeBatchByIds(idList) ? RestResult.SUCCESS() : RestResult.FAIL();
    }
    /**
     * 保存老师信息表中信息
     */
    @Operation(summary = "保存老师信息表中信息")
    @PostMapping("/tea_info/edit")
    @Parameters({
            @Parameter(name = "id",description = "id（主键）",in = ParameterIn.QUERY),
            @Parameter(name = "name",description = "姓名",in = ParameterIn.QUERY),
            @Parameter(name = "age",description = "年龄",in = ParameterIn.QUERY),
            @Parameter(name = "profession",description = "研究方向",in = ParameterIn.QUERY),
    })
    public RestResult editStudent(Teacher teacher){
        if(teacher!=null){
            //有id：修改，无id:添加
            if(teacher.getId()!=null && !teacher.getId().isEmpty()){
                return teacherService.updateById(teacher)?RestResult.SUCCESS("修改成功："+teacher):RestResult.FAIL();
            }else{
                teacher.setId(UUIDUtils.getUUID());
                return teacherService.save(teacher)?RestResult.SUCCESS("添加成功："+teacher):RestResult.FAIL();
            }
        }
        return RestResult.FAIL("传入老师对象为空，保存失败");
    }
}
